#include "tomato.h"
#include "ui_tomato.h"

#include <QTimer>
#include <QPainter>
#include <QScreen>



Tomato::Tomato(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Tomato)
{
    target = 0;

    QAction* actSkip = new QAction(tr("跳过"));

    addAction(actSkip);

    connect(actSkip, SIGNAL(triggered()), this, SLOT(skip()));

    setContextMenuPolicy(Qt::ActionsContextMenu);
}

Tomato::~Tomato()
{
    delete ui;
}

void Tomato::Draw()
{
    this->update();
}

void Tomato::paintEvent(QPaintEvent * event)
{
    int x = this->width();
    int y = this->height();
    int min= x<y?x:y;

    QPainter painter(this);
    QFont font;

    if(min>500)
        font.setPixelSize((min-20)/10);
    else
        font.setPixelSize(48);
    painter.setFont(font);
    painter.setRenderHint(QPainter:: Antialiasing, true);

    QColor color;

    switch(status){
    case R:
        color = QColor(77,81,57);
        break;
    case W:
        color = QColor(38,69,61);
        break;}

    painter.setPen(color);
    painter.setBrush(color);

    painter.drawRect(0, 0, x, y);

    painter.setPen(QPen(QColor(70,93,76),10));



    if(min > 500){
        painter.drawEllipse(x/2-min/4+25, y/2-min/4+25,min/2-50,min/2-50);
    }
    else
        painter.drawEllipse(x/2-100, y/2-100,200,200);

    painter.setPen(QPen(QColor(168,216,185),10));

    TimeCounter timer;

    if(time_count.to_uint() >= target)
        switch(status){
        case R:
            target += WORK;
            status = W;
            break;
        case W:
            target += RELAX;
            status = R;
            break;
        }

    switch(status){
    case R:
        timer.set_time(target-time_count.to_uint());
        if(min>500){
            painter.drawText(x/2-60, y/2+24, timer.to_str_MMSS());
            painter.drawArc(x/2-min/4+25, y/2-min/4+25,min/2-50,min/2-50, 16*0, 16*double(timer.to_uint())/RELAX*360);
        }
        else{
            painter.drawText(x/2-60, y/2+24, timer.to_str_MMSS());
            painter.drawArc(x/2-100, y/2-100,200,200, 16*0, 16*double(timer.to_uint())/RELAX*360);
        }
        break;
    case W:
        timer.set_time(target-time_count.to_uint());
        if(min>500){
            painter.drawText(x/2-(min+100)/10, y/2+(min-20)/20, timer.to_str_MMSS());
            painter.drawArc(x/2-min/4+25, y/2-min/4+25,min/2-50,min/2-50, 16*0, 16*double(timer.to_uint())/WORK*360);
        }
        else{
            painter.drawText(x/2-60, y/2+24, timer.to_str_MMSS());
            painter.drawArc(x/2-100, y/2-100,200,200, 16*0, 16*double(timer.to_uint())/WORK*360);
        }
        break;
    }

    event->ignore(); // 忽视原来的关闭事件
};

void Tomato::closeEvent(QCloseEvent* event)
{
    this->hide();	 // 隐藏窗口
    event->ignore(); // 忽视原来的关闭事件
}

void Tomato::skip(){
    switch(status){
    case R:
        target = time_count.to_uint() + WORK;
        status = W;
        break;

    case W:
        target = time_count.to_uint() + RELAX;
        status = R;
        break;

    }
}

void Tomato::SetDefault(unsigned int wt, unsigned int rt){
    RELAX = rt;
    WORK = wt;
};
